<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MX_Controller {
    
    private $_data = array();
    function __construct(){
        parent::__construct();
        $this->load->language('frontend');
        $this->load->model('users_model', 'users');
        $this->load->library('form_validation');
        $this->template
				->set($this->_data)
				->set_layout('default')->set_theme('default')
				->set_partial('header', 'partials/header')
				->set_partial('footer', 'partials/footer')
				->set_partial('NavTop', 'partials/top_menu');
    }
    // index == login function
    function index(){
        $this->form_validation->set_message('required', $this->lang->line('required'));
        $this->form_validation->set_message('min_length', $this->lang->line('min_length'));        
        $this->form_validation->set_rules('username', 'lang:email', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'lang:text_password', 'required|min_length[6]|md5');
        if($this->form_validation->run() == TRUE){
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $autologin = $this->input->post('remember_me');            
            $checkUser = $this->users->CheckLogin($username, $password);            
            if($checkUser){
                $this->users->table = 'profiles';
                $profile = $this->users->Select_('id = "'.$checkUser['profile_id'].'"');
                if($autologin){    
                   $cookie = array(
                       'name' => 'remember_login',
                       'value' => $profile['fullname'].'|'.$profile['id'],
                       'expire' => '86500'
                   );
                   $this->input->set_cookie($cookie);
                }
                $this->session->set_userdata('user', $profile);
                redirect('/');                
            }
        } 
        $this->template->build('users/login', $data = array());
    }
    function Logout(){
        $this->session->unset_userdata('user');
        delete_cookie('remember_login');  
        $this->session->sess_destroy();      
        redirect('/');  
    }
    function UpdateInfo(){
        
    }
    function ChangePassword(){
        echo 'change password page';
    }
    function ForgotPassword(){
        
    }
    function Register(){
        
    }
    
    
    /**
     * 
     * section call ajax
     * 
     */
    
/* ==================================

    END CLASS
    
===================================== */
}